
import numpy as np
import pandas as pd


def repeat(old, new):
    for k in new:
        if k in old:
            return False
    return True

hour = 6 # 8

calculate_path = [
    [2, 6, 4, 5, 16],
    [17, 18, 24],
    [25, 19, 14],
    [20, 7, 13],
    [12, 11, 15],
    [27, 26],
    [28, 29, 30, 23],
    [9, 10, 22, 21, 8],
    [3, 1]
]
calculate_time = [
    2.595,
    3.301,
    2.821,
    2.581,
    2.981,
    3.374,
    4.668,
    2.635,
    1.054
]


def combinations(iterable, r):
    pool = list(iterable)
    n = len(iterable)
    if r > n:
        return
    indices = list(range(r))
    yield list(pool[i] for i in indices)
    while True:
        for i in reversed(range(r)):
            if indices[i] != i + n - r:
                break
        else:
            return
        indices[i] += 1
        for j in range(i + 1, r):
            indices[j] = indices[j - 1] + 1
        yield list(pool[i] for i in indices)


path2 = []
time2 = list(combinations(calculate_time,2))
time_two = []


for i in range(len(time2)):
    temp_path = []
    for k in range(len(time2[i])):
        temp_time = np.array(time2[i])
        if temp_time.sum() <= hour:
            idx = calculate_time.index(time2[i][k])
            temp_path.append(calculate_path[idx])
            if time2[i] not in time_two:
                time_two.append(time2[i])
    if len(temp_path):
        path2.append(temp_path)

# print(path2)
# print(time_two)

# path2 = pd.DataFrame(path2)
# time_two = pd.DataFrame(time_two)

time_two_sum = [
    3.635,
    3.649,
    3.689,
    3.875,
    4.035,
    4.355,
    4.428,
    5.176,
    5.216,
    5.23,
    5.402,
    5.416,
    5.456,
    5.562,
    5.576,
    5.616,
    5.722,
    5.802,
    5.882,
    5.896,
    5.936,
    5.955,
    5.969
]
res = []
time_res = []
_temp = []
time_temp = []
for i in range(len(path2)):
    _temp.append(path2[i])
    time_temp.append(time_two_sum[i])
    stat = i+1
    for i1 in range(stat,len(path2)):
        if repeat(path2[i], path2[1]):
            stat1 = i1 + 1
            _temp.append(path2[i1])
            time_temp.append(time_two_sum[i1])
            for i2 in range(stat1,len(path2)):
                if repeat(path2[i], path2[i2]) and repeat(path2[i1],path2[i2]):
                    stat2 = i2 + 1
                    _temp.append(path2[i2])
                    time_temp.append(time_two_sum[i2])
                    for i3 in range(stat2,len(path2)):
                        if repeat(path2[i], path2[i3]) and repeat(path2[i1], path2[i3]) and repeat(path2[i2], path2[i3]):
                            _temp.append(path2[i3])
                            time_temp.append(time_two_sum[i3])
                            for one in range(len(calculate_path)):
                                if repeat(path2[i], calculate_path[one]) and repeat(path2[i1], calculate_path[one]) and repeat(path2[i2], calculate_path[one]) and repeat(path2[i3],calculate_path[one]):
                                    _temp.append(calculate_path[one])
                                    time_temp.append(calculate_time[one])
                                    res_temp = _temp.copy()
                                    res.append(res_temp)
                                    time_res_temp = time_temp.copy()
                                    time_res.append(time_res_temp)
                                    _temp.pop()
                                    time_temp.pop()
                            _temp.pop()
                            time_temp.pop()
                    _temp.pop()
                    time_temp.pop()
            _temp.pop()
            time_temp.pop()
    _temp.pop()
    time_temp.pop()

res = pd.DataFrame(res)
jicha = []

for g in time_res:
    g = np.array(g)
    l = len(g)
    g_min = g.min()
    g = g-g_min
    chazhi = g.sum()/l
    jicha.append(chazhi)

jicha = pd.DataFrame(jicha)

path3 = []
time3 = list(combinations(calculate_time,3))
time_tree = []
for i in range(len(time3)):
    temp_path = []
    for k in range(len(time3[i])):
        temp_time = np.array(time3[i])
        if temp_time.sum() <= hour:
            idx = calculate_time.index(time3[i][k])
            temp_path.append(calculate_path[idx])
            if time3[i] not in time_tree:
                time_tree.append(time3[i])
    if len(temp_path):
        path3.append(temp_path)

# print(path3)
# print(time_tree)

# path3 = pd.DataFrame(path3)
# time_tree = pd.DataFrame(time_tree)


time2_sum = [
    5.896,
    5.416,
    5.176,
    5.576,
    5.969,
    7.263,
    5.23,
    3.649,
    6.122,
    5.882,
    6.282,
    6.675,
    7.969,
    5.936,
    4.355,
    5.402,
    5.802,
    6.195,
    7.489,
    5.456,
    3.875,
    5.562,
    5.955,
    7.249,
    5.216,
    3.635,
    6.355,
    7.649,
    5.616,
    4.035,
    6.009,
    4.428,
    7.303,
    5.722,
    3.689
]
time3_sum = [
    6.95,
    7.997,
    6.47,
    7.811,
    6.23,
    6.63,
    7.023,
    6.284,
    7.176,
    6.936,
    7.336,
    7.729,
    6.99,
    6.456,
    6.856,
    7.249,
    6.51,
    6.616,
    7.009,
    6.27,
    7.409,
    6.67,
    7.063
]

"""3+2+2+1"""
res = []
time_res = []
_temp = []
time_temp = []
"""
for three in range(len(path3)):
    _temp.append(path3[three])
    time_temp.append(time3_sum[three])
    for two in range(len(path2)):
        if repeat(path3[three], path2[two]):
            start = two + 1
            _temp.append(path2[two])
            time_temp.append(time2_sum[two])
            for twos in range(start, len(path2)):
                if repeat(path2[two], path2[twos]) and repeat(path3[three], path2[twos]):
                    _temp.append(path2[twos])
                    time_temp.append(time2_sum[twos])
                    for one in range(len(calculate_path)):
                        if repeat(path2[twos], calculate_path[one]) and repeat(path2[two], calculate_path[one]) and repeat(path3[three], calculate_path[one]):
                            _temp.append(calculate_path[one])
                            time_temp.append(calculate_time[one])
                            res_temp = _temp.copy()
                            res.append(res_temp)
                            time_res_temp = time_temp.copy()
                            time_res.append(time_res_temp)
                            time_temp.pop()
                            _temp.pop()
                    time_temp.pop()
                    _temp.pop()
            time_temp.pop()
            _temp.pop()
    time_temp.pop()
    _temp.pop()
"""
"""
3+3+2+1
"""
for three in range(len(path3)):
    _temp.append(path3[three])
    time_temp.append(time3_sum[three])
    start = three + 1
    for threes in range(start, len(path3)):
        if repeat(path3[three], path3[threes]):
            _temp.append(path3[three])
            time_temp.append(time3_sum[threes])
            for two in range(len(path2)):
                if repeat(path3[three], path2[two]) and repeat(path3[threes], path2[two]):
                    _temp.append(path2[two])
                    time_temp.append(time2_sum[two])
                    for one in range(len(calculate_path)):
                        if repeat(path2[two], calculate_path[one]) and repeat(path3[three], calculate_path[one]) and repeat(path3[threes], calculate_path[one]):
                            _temp.append(calculate_path[one])
                            time_temp.append(calculate_time[one])
                            res_temp = _temp.copy()
                            res.append(res_temp)
                            time_res_temp = time_temp.copy()
                            time_res.append(time_res_temp)
                            time_temp.pop()
                            _temp.pop()
                    time_temp.pop()
                    _temp.pop()
            time_temp.pop()
            _temp.pop()
    time_temp.pop()
    _temp.pop()





res = pd.DataFrame(res)
print(res)

jicha = []

for g in time_res:
    g = np.array(g)
    l = len(g)
    g_min = g.min()
    g = g-g_min
    chazhi = g.sum()/l
    jicha.append(chazhi)

jicha = pd.DataFrame(jicha)

time_res = pd.DataFrame(time_res)